package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.iwhalecloud.bss.hsf.baseinf.service.InfExchangeService;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.callclient.HttpsCallClient;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalInfConsts;
import com.iwhalecloud.bss.kite.cucc.service.inf.InfServiceHelper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 付喆
 * @version 2021/5/24 15:07
 * Description: 主用电路业务号码
 */
public class MainCircuitSerialNumberHandler extends AbstractAttrHandler<KiteProdInst, CustAttrData> {
    @Value("${interface.call.mode:hsf}")
    private String mode;
    @Autowired
    IQuerySceneInstService querySceneInstService;
    @Override
    public void after(KiteAttr attr, KiteProdInst inst, CustAttrData data){
        super.after(attr, inst, data);
        String custId = data.getCustId();
        String offerInstId = querySceneInstService.getSceneInst(data.getSceneInstId()).getRootInstId();
        KiteOfferInst kiteOfferInst = querySceneInstService.getOfferInst(data.getCustId(), offerInstId);
        String goodsId = kiteOfferInst.getOfferId();
        List<String> serialNumberList = queryCustIdByAccNum(custId, goodsId);
        List<KiteAttrValue> kiteAttrValueList = serialNumberList.stream().map(serialNumber -> {
            KiteAttrValue kiteAttrValue = new KiteAttrValue();
            kiteAttrValue.setAttrId("200002085");
            kiteAttrValue.setAttrValue(serialNumber);
            kiteAttrValue.setAttrValueDesc(serialNumber);
            kiteAttrValue.setAttrValueName(serialNumber);
            kiteAttrValue.setIsShow(KeyConsts.IFTRUE_T);
            return kiteAttrValue;
        }).collect(Collectors.toList());
        inst.getAttr(attr.getFieldName()).setAttrValues(kiteAttrValueList);
    }
    public List<String> queryCustIdByAccNum(String custId, String goodsId) {
        String staffCode = (String) ContextUtil.getLoginInfo().getUserInfo().getExtParams().get("account");
        String pageNum = "1";
        String pageSize = "50";
        Map<String,Object> requestMap = new HashMap<String,Object>();
        requestMap.put("OP_CODE", "json_bbssUserCenter_SalBusaGrpUserSerApi_qryGrpUserList");
        Map<String,Object> uniBssBodyMap = new HashMap<String,Object>();
        Map<String,Object> reqMap = new HashMap<String,Object>();
        reqMap.put("STAFF_CODE", staffCode);
        reqMap.put("CUST_ID", custId);
        reqMap.put("QRY_TYPE", "2");
        reqMap.put("QRY_SOURCE", "2");
        reqMap.put("PAGE_NUM", pageNum);
        reqMap.put("PAGE_SIZE", pageSize);
        reqMap.put("GOODS_ID", goodsId);

        //tcpCont
        Map<String,String> tcpContMap = new HashMap<String,String>();
        tcpContMap.put("APP_KEY", "");
        tcpContMap.put("TRANS_ID", "");
        reqMap.put("TCP_CONT", tcpContMap);

        //para
        List<Map<String,String>> para = new ArrayList<Map<String,String>>();
        reqMap.put("PARA", para);
        uniBssBodyMap.put("REQ", reqMap);
        requestMap.put("UNI_BSS_BODY", uniBssBodyMap);
        InfServiceHelper.adaptGray(requestMap, reqMap);
        long time21 = System.currentTimeMillis();
        Map map;
        if (KiteStringUtils.isEqual(mode, "http")) {
            map = HttpsCallClient.postObjMap(LocalCallClientEnum.QRY_PRODINST_LIST.getCenterCode(),
                LocalCallClientEnum.QRY_PRODINST_LIST.getServiceCode(), requestMap, null);
        }
        else {
            InfExchangeService infExchangeService = SpringUtil.getBean("infExchangeService");
            String resultStr = infExchangeService.exchange(new JSONObject(requestMap));
            map = JSON.parseObject(resultStr, Map.class);
        }
        Map<String,Object> unibssHeadMap = (Map<String,Object>)map.get("UNI_BSS_HEAD");
        if (!KiteStringUtils.equals(KiteMapUtils.getString(unibssHeadMap, "RESP_CODE"), LocalInfConsts.RESP_SUCCESS)){
            ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("json_bbssUserCenter_SalBusaGrpUserSerApi_qryGrpUserList",KiteMapUtils.getString(unibssHeadMap, "RESP_DESC"));
        }

        Map<String,Object> rspUniBssBodyMap = (Map<String,Object>)map.get("UNI_BSS_BODY");

        if(KiteMapUtils.isEmpty(rspUniBssBodyMap)){
            ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("返回报文缺少UNI_BSS_BODY节点");
        }
        Map<String,Object> rspMap = (Map<String,Object>)rspUniBssBodyMap.get("RSP");
        if (!KiteStringUtils.equals(KiteMapUtils.getString(rspMap,"RSP_CODE"), "0000")){
            ErrorConsts.INF_INVOKE_EXCEPTION.throwOut("json_bbssUserCenter_SalBusaGrpUserSerApi_qryGrpUserList",KiteMapUtils.getString(rspMap, "RSP_DESC"));
        }
        Map<String,Object> qryGrpUserListMap = (Map<String,Object>)rspMap.get("QRY_GRP_USER_LIST_RSP_BODY");

        List<JSONObject> groupUserList = (List<JSONObject>)qryGrpUserListMap.get("GROUP_USER_LIST");
        if(KiteListUtils.isEmpty(groupUserList)){
            return Lists.newArrayList();
        }
        return groupUserList.stream().map(item -> item.getString("SERIAL_NUMBER")).collect(Collectors.toList());
    }
}
